/*
 * @Author: 0x9DEFA478
 * @Date: 2021-09-30 16:10:37
 * @LastEditTime: 2021-10-11 20:48:40
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "I_VCOM.h"
#include "H_ThreadScheduler.h"
#include "Peripherals_Instence.h"
#include "MemoryMalloc.h"
#include "H_FIFO.h"
#include "H_String.h"
#include "usbd_class_if.h"
#include "I_NFC.h"

#define vI_VCOM_SingleTransmitSize 64

#define vI_VCOM_RecvFIFO_SizeArraySize 8
#define vI_VCOM_RecvFIFO_BufferSize    (64*(vI_VCOM_RecvFIFO_SizeArraySize+1))


struct{

  
  H_FIFO* RecvFIFO;

  H_TS_Semaphore* USBINDoneSemaphore;
  H_TS_Semaphore* USBOUTDoneSemaphore;


}I_VCOM;





/**
 * @brief I_VCOM的数据传输到主机完成调用
 * @return 无
 */
void I_VCOM_InDone_Callback(){

  H_TS_SemaphoreGive_ISR(I_VCOM.USBINDoneSemaphore);
}

/**
 * @brief 主机数据传入时调用
 * @param Data 数据指针
 * @param Size 数据大小
 * @return 无
 */
void I_VCOM_OutDone_Callback(unsigned char* Data,int Size){
  if(0==H_FIFO_Write(I_VCOM.RecvFIFO,Data,Size)){
    H_TS_SemaphoreGive_ISR(I_VCOM.USBOUTDoneSemaphore);
  }
}

/**
 * @brief 向主机发送数据
 * @param Data 数据指针
 * @param Size 数据大小
 * @return 无
 */
void I_VCOM_In(unsigned char* Data,int Size){
  int length;


  while(usbd_class_IsConnect()==0){
    H_TS_ThreadSleep(1);
  }

  H_TS_Locking(ip_usb_Lock);

  
  length=0;
  while(Size>=vI_VCOM_SingleTransmitSize){
    length=vI_VCOM_SingleTransmitSize;
    Size-=vI_VCOM_SingleTransmitSize;

    usbd_class_CDC_Send(Data,length);
    H_TS_SemaphoreTake(I_VCOM.USBINDoneSemaphore);

    Data=&Data[vI_VCOM_SingleTransmitSize];
  }
  if(Size>0){
    length=Size;
    usbd_class_CDC_Send(Data,length);
    H_TS_SemaphoreTake(I_VCOM.USBINDoneSemaphore);
  }

  if(length==vI_VCOM_SingleTransmitSize){
    usbd_class_CDC_Send(Data,0);
    H_TS_SemaphoreTake(I_VCOM.USBINDoneSemaphore);
  }

  H_TS_UnLock(ip_usb_Lock);
  
}

H_TS_Thread* I_VCOM_Out_ThreadHandle;
static int I_VCOM_Out_Thread(void* v){
  unsigned char* readBuffer;
  int readLength;
  H_Malloc_Info info;
  unsigned char strBuffer[64];
  char* str;
  int value;
  char* numstr;
  int lastLen;
  int isN;

  for(;;){
    H_TS_SemaphoreTake(I_VCOM.USBOUTDoneSemaphore);
    readBuffer=H_FIFO_Read(I_VCOM.RecvFIFO,&readLength);
    if(readBuffer!=NULL){
      
      //接收到数据

      if(readBuffer[0]=='i'){
        MemoryMalloc_SRAM_GetInfo(&info);

        str=H_String(strBuffer,"SRAM_Free:{i}Byte\r\n",info.FreeSize);
        I_VCOM_In(strBuffer,H_StringGetLength(str));

        str=H_String(strBuffer,"StackLoad:{f.2}%\r\n",(float)H_TS_GetStackUtilization()/10.0f);
        I_VCOM_In(strBuffer,H_StringGetLength(str));
      }

      

      if(readBuffer[0]=='v'){
        I_NFC_SnedCMD(NULL,NULL);
      }

      if(readBuffer[0]=='D'){
        I_NFC_SetMode(vI_NFC_Mode_DeInit,0);
      }

      if(readBuffer[0]=='I'){
        I_NFC_SetMode(vI_NFC_Mode_Init,0);
      }

      if(readBuffer[0]=='R'){
        I_NFC_SetMode(vI_NFC_Mode_Read,0);
      }

      if(readBuffer[0]=='C'){

        numstr=&readBuffer[1];
        lastLen=readLength-1;

        if(lastLen>1){
          if(numstr[0]=='-'){
            isN=-1;
            numstr=&numstr[1];
            lastLen--;
          }else{
            isN=1;
          }

          value=0;
          while(lastLen>0){
            lastLen--;
            value=value*10+numstr[0]-'0';
            numstr=&numstr[1];
          }

          I_NFC_SetMode(vI_NFC_Mode_ValueChange,isN*value);
        }

        
      }


      H_FIFO_Next(I_VCOM.RecvFIFO);
    }
  }
}

/**
 * @brief 初始化I_VCOM实例
 * @return 无
 */
void I_VCOM_Init(){
  unsigned char* Buffer;
  int* SizeArray;

  I_VCOM.USBINDoneSemaphore=new_H_TS_Semaphore(1);

  I_VCOM.USBOUTDoneSemaphore=new_H_TS_Semaphore(vI_VCOM_RecvFIFO_SizeArraySize);

  if((I_VCOM.USBINDoneSemaphore==NULL)||(I_VCOM.USBOUTDoneSemaphore==NULL)){
    for(;;){
    }
  }

  I_VCOM.RecvFIFO=MemoryMalloc_Malloc(sizeof(H_FIFO));
  if(I_VCOM.RecvFIFO==NULL){
    for(;;){
    }
  }

  Buffer=MemoryMalloc_Malloc(vI_VCOM_RecvFIFO_BufferSize);
  SizeArray=MemoryMalloc_Malloc(4*vI_VCOM_RecvFIFO_SizeArraySize);
  if((Buffer==NULL)||(SizeArray==NULL)){
    for(;;){
    }
  }

  H_FIFO_Init(I_VCOM.RecvFIFO,vH_FIFO_Type_byte,Buffer,vI_VCOM_RecvFIFO_BufferSize,SizeArray,vI_VCOM_RecvFIFO_SizeArraySize);

  I_VCOM_Out_ThreadHandle=H_TS_StartNewThread(I_VCOM_Out_Thread,NULL,1024,-16,0);
  if(I_VCOM_Out_ThreadHandle==NULL){
    for(;;){
    }
  }
  I_VCOM_Out_ThreadHandle->ThreadName="I_VCOM_Out_Thread";


}






